/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.controllers;

import cz.muni.fi.pa036is.dto.LoginDTO;
import cz.muni.fi.pa036is.dto.MatchGridTO;
import cz.muni.fi.pa036is.dto.SeasonDetailTO;
import cz.muni.fi.pa036is.dto.SeasonFormTO;
import cz.muni.fi.pa036is.servicesinterface.MatchService;
import cz.muni.fi.pa036is.servicesinterface.PersonService;
import cz.muni.fi.pa036is.servicesinterface.SeasonService;
import cz.muni.fi.pa036is.utilities.DateEditor;
import cz.muni.fi.pa036is.validators.SeasonValidator;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes("season")
public class SeasonController {

    @Autowired
    private SeasonService seasonService;
    @Autowired
    private MatchService matchService;
	@Autowired
	private PersonService personService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateEditor());
    }

    // List
    @RequestMapping("/admin/season/season")
    public String seasons(ModelMap model) {

        model.addAttribute("login", new LoginDTO());
        model.addAttribute("seasons", seasonService.getAllSeasons());
        return "/admin/season/season";
    }

    // Detail
    @RequestMapping(value = "/admin/season/detail", params = "id")
    public String detail(@RequestParam("id") Long id, ModelMap model) {
        SeasonDetailTO season = this.seasonService.getSeasonDetail(id);

        List<MatchGridTO> matches = this.matchService.getMatchesBySeason(id);

        model.addAttribute("season", season);
        model.addAttribute("matches", matches);
		model.addAttribute("persons", this.personService.getPersonListByScore(id));
        model.addAttribute("login", new LoginDTO());

        return "/admin/season/detail";
    }

    // Edit
    @RequestMapping(value = "/admin/season/edit", params = "id")
    public String edit(@RequestParam("id") Long id, ModelMap model) {


        SeasonFormTO season = null;
        if (id == null || id < 0) {
            season = new SeasonFormTO();
        } else {
            season = this.seasonService.getSeasonForm(id);
        }

        model.addAttribute("season", season);

        return "/admin/season/edit";
    }

    @RequestMapping(value = "/admin/season/edit", method = RequestMethod.POST)
    public String edit(@ModelAttribute("season") SeasonFormTO season, BindingResult result, ModelMap model,
            HttpSession session) {

       SeasonValidator seasonValidator = new SeasonValidator();
       seasonValidator.validate(season, result);
       
        if (result.hasErrors()) {
            
            HashMap<String, String> errors = new HashMap<String, String>();
            for (FieldError error : result.getFieldErrors()) {
                errors.put(error.getField(), error.getDefaultMessage());
            }
            model.put("errors", errors);
            for (ObjectError o: result.getAllErrors()) {
                System.err.println(o);
                
            }
                
            
            model.put("season", season);
            return "/admin/season/edit";
        }

        if (season.getId() == null || season.getId() < 0) {
            this.seasonService.addSeason(season);
        } else {
            this.seasonService.editSeason(season);
        }


        return "redirect:/admin/season/detail?id=" + season.getId();
    }

    @RequestMapping(value = "/admin/season/remove", params = "id")
    public String remove(@RequestParam("id") Long id, ModelMap model, HttpSession session) {

        seasonService.deleteSeason(id);
        return "redirect:/admin/season/season";
    }
}
